﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Utils;

namespace COFE.Core.Filter
{
    [EntryFilter("guid")]
    public class GuidEntryFilter : EntryFilterBase
    {
        public GuidEntryFilter(Guid guid, string id = "guid")
            : base(guid.ToString(), id)
        {
            SupportedMatchType = MatchType.MatchThisEntry;
            _guidList.Add(guid);
        }

        private static string guidsToString(Guid[] guids)
        {
            return String.Join(",", 
                (from g in guids select g.ToString()).ToArray()) ;
        }

        private static Guid[] stringToGuids(string guidStr)
        {
            return (from s in guidStr.Split(',')
                   select new Guid(s)).ToArray();
        }

        public GuidEntryFilter(Guid[] guids, string id = "guid")
            : base(guidsToString(guids), id)
        {
            SupportedMatchType = MatchType.MatchThisEntry;
            _guidList.AddRange(guids);
        }

        public GuidEntryFilter(string guidStr, string id = "guid")
            : this(stringToGuids(guidStr), id)
        {            
        }

        private List<Guid> _guidList = new List<Guid>();
        public Guid[] LookupGuids { get { return _guidList.ToArray(); } }

        public override MatchType matches(IInternalFileSystemInfoExA entry,
            params IOptionFilter[] options)
        {
            bool match = _guidList.Contains(entry.GetId());

            return match ? MatchType.MatchThisEntry : MatchType.None;
        }
        
    }
}
